package com.dyh.quickdemozwei.controller0824;

import java.util.concurrent.locks.ReentrantLock;

public class OneWorkerController {
    volatile WorkerThread workerThread =null;
    ReentrantLock workerThreadLock = new ReentrantLock();

    static class WorkerThread extends Thread{

        Object in;

        public WorkerThread(Object in) {
            this.in = in;
        }

        @Override
        public void run() {
            System.out.println("开始"+in);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("结束"+in);
        }
    }

    public void work(Object in){
        if (workerThread==null){
            if (workerThreadLock.tryLock()){
                if (workerThread==null){
                    workerThread = new WorkerThread(in);
                    workerThread.start();
                }
                else System.out.println("已经在处理中请稍后再试-1");

                workerThreadLock.unlock();
            }
            else System.out.println("已经在处理中请稍后再试-2");

        }
        else System.out.println("已经在处理中请稍后再试-3");

    }

    public static void main(String[] args) throws InterruptedException {
//        OneWorkerController oneWorkerController = new OneWorkerController();
//
//
//        Thread t1 = new Thread(()-> oneWorkerController.work(1));
//        Thread t2 = new Thread(()-> oneWorkerController.work(2));
//        Thread.sleep(20L);
//        Thread t3 = new Thread(()-> oneWorkerController.work(3));
//
//
//        t1.start();
//        t2.start();
//        t3.start();
//
//        Thread.sleep(5000L);


        Long l = null;
        System.out.println(String.valueOf(l));
    }


}
